import { EditOutlined, DeleteOutlined } from '@ant-design/icons'
import {
  Button, DatePicker, Table, Tag, Space,
  Form,
  Popconfirm,
  Radio,
  Select,
  Card,
  Breadcrumb,
  message, Image
} from 'antd'
import { useEffect, useState } from 'react'
import { Link, useLocation, useNavigate } from 'react-router-dom'
import img404 from '@/assets/error.png'
import { http } from '@/utils'
import { useStore } from '@/store'
import { observer } from 'mobx-react-lite'
const { RangePicker } = DatePicker
const { Option } = Select

const Article = () => {
  //频道列表管理
  const { channelStore } = useStore()
  //文章列表管理 
  const [articleData, setArtileData] = useState({
    list: [], //文章列表
    count: 0 //文章数量
  })
  //文章参数管理
  const [params, setParamrs] = useState({
    page: 1,
    per_page: 10
  })

  useEffect(() => {
    const loadList = async () => {
      let res = await http.get('/mp/articles', { params })
      const { results, total_count } = res.data
      // console.log(res)
      setArtileData({
        list: results,
        count: total_count
      })
    }
    loadList()
  }, [params])
  const onFinish = (values) => {
    const { channel_id, date, status } = values
    //数据处理
    const checkValue = {}
    if (status !== -1) {
      checkValue.status = status
    }
    if (channel_id) {
      checkValue.channel_id = channel_id
    }
    if (date) {
      checkValue.begin_pubdate = date[0].format('YYYY-MM-DD')
      checkValue.end_pubdate = date[1].format('YYYY-MM-DD')
    }
    //修改params数据，引起接口的重新发送  对象的合并是一个整体覆盖 改了对象整体引用
    setParamrs({
      ...params,
      ...checkValue
    })
  }
  const pageChange = (page) => {
    setParamrs({
      ...params,
      page
    })
  }
  const delArticle = async (data) => {
    await http.delete(`/mp/articles/${data.id}`)
    setParamrs({
      ...params,
      page: 1
    })
    message.success("删除成功")
  }
  const navigate = useNavigate()
  const goPublish = (data) => {
    navigate(`/publish?id=${data.id}`)
  }
  const columns = [
    {
      title: '封面',
      dataIndex: 'cover',
      width: 120,
      render: cover => {
        return <Image
          width={100}
          height={100}
          src={cover ? cover.images : img404}
        />
      }
    },
    {
      title: '标题',
      dataIndex: 'title',
      width: 200
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: data => <Tag color="green">审核通过</Tag>
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate'
    },
    {
      title: '阅读数',
      dataIndex: 'read_count'
    },
    {
      title: '评论数',
      dataIndex: 'comment_count'
    },
    {
      title: '点赞数',
      dataIndex: 'like_count'
    },
    {
      title: '操作',
      render: data => {
        return (
          <Space size="middle">
            <Button
              onClick={() => goPublish(data)}
              type="primary" shape="circle" icon={<EditOutlined />} />
            <Popconfirm
              title="删除文章"
              description="你确定删除该文章?"
              onConfirm={() => delArticle(data)}
              okText="是"
              cancelText="否"
            >
              <Button
                type="primary"
                danger
                shape="circle"
                icon={<DeleteOutlined />}
              />
            </Popconfirm>

          </Space>
        )
      }
    }
  ]
  return (
    <div >
      <Card
        title={
          <Breadcrumb
            items={[
              {
                title: <Link to="/home">首页</Link>
              },
              {
                title: '内容管理'
              }
            ]}
          >
          </Breadcrumb>
        }
        style={{ marginBottom: 20 }}
      >
        <Form
          initialValues={{ status: -1 }}
          onFinish={onFinish}
        >
          <Form.Item label="状态" name="status" style={{ textAlign: 'left' }}>
            <Radio.Group>
              <Radio value={-1}>全部</Radio>
              <Radio value={0}>草稿</Radio>
              <Radio value={1}>待审核</Radio>
              <Radio value={2}>审核通过</Radio>
              <Radio value={3}>审核失败</Radio>
            </Radio.Group>
          </Form.Item>
          <Form.Item label="频道" name="channel_id" style={{ textAlign: 'left' }}>
            <Select placeholder="请选择文章频道"
              style={{ width: 120 }}>
              {channelStore.channelList.map(item => <Option key={item.id} value={item.id}>{item.name}</Option>)}

            </Select>
          </Form.Item>
          <Form.Item label="日期" name="date" style={{ textAlign: 'left' }}>
            <RangePicker />
          </Form.Item>
          <Form.Item style={{ textAlign: 'left' }}>
            <Button type="primary" htmlType="submit" style={{ marginLeft: 80 }}>
              筛选
            </Button>
          </Form.Item>
        </Form>
      </Card>
      <Card style={{ textAlign: 'left' }} title={`根据筛选条件共查询到 ${articleData.count} 条结果：`}>
        <Table rowKey="id" columns={columns} dataSource={articleData.list}
          pagination={{
            pageSize: params.per_page,
            total: articleData.count,
            onChange: pageChange
          }}
        />
      </Card>
    </div>
  )
}

export default observer(Article) 